package com.robusta.cityuate.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.robusta.cityuate.R;
import com.robusta.cityuate.objects.Reservation;
import com.robusta.cityuate.utils.TimeUtils;

public class ReservationsListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private ArrayList<Reservation> mEntries = new ArrayList<Reservation>();
	private Context mContext;

	public ReservationsListAdapter(Context context) {
		super();
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return mEntries.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntries.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView;
		final Reservation res = mEntries.get(position);
		
		if (convertView == null)
			itemView = mLayoutInflater.inflate(R.layout.reservation_list_item,
					parent, false);
		else
			itemView = convertView;

		TextView spotNameField = (TextView) itemView.findViewById(R.id.spot_name);
		TextView fromField = (TextView) itemView
				.findViewById(R.id.available_at);
		TextView tillField = (TextView) itemView
				.findViewById(R.id.available_till);
		
		spotNameField.setText(res.getSpot().getName());
		fromField.setText(TimeUtils.formatDate(res.getCheckInTime(), TimeUtils.DATE_TIME_FORMAT));
		tillField.setText(TimeUtils.formatDate(res.getCheckoutTime(), TimeUtils.DATE_TIME_FORMAT));

		return itemView;
	}
	
	
	/**
	 * update list of spots after receiving them from service
	 * 
	 * @param entries
	 */
	@SuppressWarnings("unchecked")
	public void upDateEntries(ArrayList<Reservation> entries) {
		mEntries = (ArrayList<Reservation>) entries.clone();
		notifyDataSetChanged();
	}
	
	
}
